package com.lance.answer.question_export;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Sets;
import com.lance.answer.gui.answer.AnswerObject;
import org.apache.commons.io.IOUtils;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @author yixun
 * @version 2.0.0
 * @description  发现两个答案的差异
 * @date 2021-06-08 00:18
 * @since 2.0.0
 */
public class AnswerDiff {

    public static void main(String[] args) throws IOException {
        diff("人体解剖生理学/03427.answer1", "人体解剖生理学/03427.answer2");
    }

    public static void diff(String path1,String path2) throws IOException {
        //获取模板路径,通过流的方式获取文件可以避免被打成jar包之后无法获取文件路径的异常
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path1);
        String answerStr1 = IOUtils.toString(inputStream);
        List<AnswerObject> list = JSONObject.parseArray(answerStr1,AnswerObject.class);
        Map<String,AnswerObject> map = new HashMap<>();
        for (AnswerObject answerObject:list) {
            map.put(answerObject.getQuestionCode(),answerObject);
        }
        System.out.println(map.size());
        InputStream inputStream2 = Thread.currentThread().getContextClassLoader().getResourceAsStream(path2);
        String answerStr2 = IOUtils.toString(inputStream2);
        List<AnswerObject> list2 = JSONObject.parseArray(answerStr2,AnswerObject.class);
        Map<String,AnswerObject> map2 = new HashMap<>();
        for (AnswerObject answerObject:list2) {
            map2.put(answerObject.getQuestionCode(),answerObject);
        }
        System.out.println(map2.size());
        Set<String> difference = difference(map.keySet(),map2.keySet());
        System.out.println(difference.size());
        System.out.println(difference.toString()
        );

    }

    public static Set<String> difference(Set<String> source, Set<String> target) {
        Sets.SetView<String> diff1 = Sets.difference(source, target);
        Sets.SetView<String> diff2 = Sets.difference(target, source);
        if (CollectionUtils.isEmpty(diff1) && CollectionUtils.isEmpty(diff2)) {
            return Collections.emptySet();
        }
        Set<String> diffSet = new HashSet<>();
        if (!CollectionUtils.isEmpty(diff1)) {
            diffSet.addAll(diff1);
        }
        if (!CollectionUtils.isEmpty(diff2)) {
            diffSet.addAll(diff2);
        }
        return diffSet;
    }

}
